package com.demo.tool;


import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;



//接口返回结果集
public class ReturnResult<T> implements Serializable {

   //状态码
    private  Integer code;

    //成功状态
    private Boolean success;

    //提示信息
    private  String message;

    //返回的数据
    private T data;

    //请求时间
    private String time;


    public ReturnResult(Integer code,Boolean success,String message,T data){
       this.code=code;
       this.success=success;
       this.message=message;
       if(data!=null){
        this.data=data;
       }else {
           this.data=(T)new String();
       }
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      this.time=simpleDateFormat.format(new Date());
    }


    //根据传入的值判断状态
    public static ReturnResult isSuccessOrError(Boolean result){

        return result==true?new ReturnResult(20000,true,"操作成功",null):new ReturnResult(200,false,"操作失败",null);
    }

    //success状态
    public static  ReturnResult success(){
        return new ReturnResult(20000,true,"操作成功",null);
    }


   //success状态带数据
    public static <V> ReturnResult success(V data){
     return new ReturnResult(20000,true,"操作成功",data);
    }

    //error状态
    public static  ReturnResult error(String message){
        return new ReturnResult(422,false,message,null);
    }

    //error状态
    public static  ReturnResult error(String message,Integer code){
        return new ReturnResult(code,false,message,null);
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }


}
